package com.itfreer.content.task;

import android.util.Log;

import com.itfreer.content.ContentDatabaseHelper;
import com.itfreer.core.config.ConfigManager;
import com.itfreer.core.http.DataService;
import com.itfreer.core.security.UserInfoEntity;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.misc.TransactionManager;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * 任务服务接口，用于任务数据同步与数据展示
 */
public class TaskService {

    private static final String noticeSN = "locuswork";
    private static final Integer pageSize = 100;

    public static final TaskService instance = new TaskService();
    private TaskService() {

    }

    /**
     * 获取所有信息
     * @return
     */
    public List<TaskEntity> getEntitys(Map map) {
        try {
            Dao<TaskEntity, String> dao = ContentDatabaseHelper.getInstance().getDao(TaskEntity.class);
            List<TaskEntity> datas;
            if(map==null){
                datas = dao.queryForAll();
            }else{
                datas = getDefaultData(map);
            }
            return datas;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 同步通知
     * @return
     */
    public Boolean syn() {
        UserInfoEntity userEntity = ConfigManager.getUser();
        if(userEntity==null) {
            return false;
        }

        //创建事务管理器
        TransactionManager transactionManager = new TransactionManager(ContentDatabaseHelper.getInstance().getConnectionSource());
        //一个调用的事件
        Callable<Boolean> callable  = new Callable<Boolean>() {
            //如果异常被抛出 事件管理 就知道保存数据失败要回滚
            @Override
            public Boolean call() throws Exception {
                Dao<TaskEntity, String> dao = ContentDatabaseHelper.getInstance().getDao(TaskEntity.class);
                // 清空表
                dao.queryRaw("delete from gather_task");

                List<TaskEntity> datas = getDefaultData(null);
                if(datas!=null && datas.size()>0){
                    for(TaskEntity item : datas){
                        dao.create(item);
                    }
                }
                return true;
            }
        };

        Boolean result;
        try {
            //执行事件
            result =  transactionManager.callInTransaction(callable);
        } catch (SQLException e) {
            result = false;
            Log.w("YKF","事务保存异常");
            e.printStackTrace();
        }
        return result;
    }

    private List<TaskEntity> getDefaultData(Map map) {
        HashMap<String, Integer> order = new HashMap<>();
        order.put("status", 1);
        order.put("createTime", 1);

        HashMap<String, Object> where = new HashMap<>();
        if(ConfigManager.getUser() != null) {
            where.put("ower:like", ConfigManager.getUser().getUserId());// 默认查询当前用户数据
        }

        if (map != null) {
            Date begin = (Date) map.get("begin");
            Date end = (Date) map.get("end");
            if (begin != null && end != null) {
                // 查询时间段数据
//                where.put("date:>=", begin);
//                where.put("date:<=", end);
            }
        }

        List<TaskEntity> datas = DataService.instance.getEntitys(noticeSN,null,where,order, pageSize, 1, TaskEntity.class);

        return datas;
    }
}
